<?php
    ##################################################
    ##  模块名：分页类                              ##
    ##  功能描述：                                  ##
    ##  作者：Jufei jufeis@gmail.com                ##
    ##                                              ##
    ##  创建日期：2006-11-11                        ##
    ##  修改日期：2006-12-13                        ##
    ##                                              ##
    ##################################################


class Helper_Pager {

//---公有数据---//

	public  $display;    //分页的显示字串
	public  $minimum;    //分页的起始记录
	public  $pagesize;	 //每页的记录数
	public $buttonArray = array("首页","上页","下页","末页");

	 /* 分页函数
	  * $total 记录总数
	  * $pagesize 每页的记录数
	  * $page 当前第几页，默认为第1页
	  * $extra_url 分页的url附加的Get参数。
	  */
	function __construct($total, $pagesize, $page = 1, $extra_url=''){
		
		$ButtonArray = $this->buttonArray;
		if($page < 1) {
			$page = 1;
		}
		$totalpage = @ceil($total/$pagesize); //计算总页数

		$filename = basename($_SERVER["PHP_SELF"]); //当前文件名
		$nextpage = $page+1;
		$uppage = $page-1;
		if($nextpage > $totalpage) $nextpage = $totalpage;
		if($uppage < 1) $uppage = 1;

		$options = "";
		for ($i=1;$i<=$totalpage; $i++){
			$selected = $i == $page ? "selected" : "";
			$options .= "<option value=$i $selected>$i</option>";
		}
		
		$page_url_pre = "$filename?" . self::clearQuery($_SERVER['QUERY_STRING'] . "&$extra_url",array('page')) . "&page=";
		//分页的显示表格
		$showpage = "<TABLE width='100%' border=0 cellspacing=0 cellpadding=0>
				   <TR>
						<TD height='18' align=right valign='bottom' nowrap>共有 {$total} 条记录  &nbsp;&nbsp;  $page / $totalpage  <a href='{$page_url_pre}1'>$ButtonArray[0]</a>|<a href='{$page_url_pre}$uppage'>$ButtonArray[1]</a>|<a href='{$page_url_pre}$nextpage'>$ButtonArray[2]</a>|<a href='{$page_url_pre}$totalpage'>$ButtonArray[3]</a>
						转到第 <select name='page' style='margin:0px' onchange='location.href=\"$page_url_pre\"+this.value'>$options</select> 页 
						</TD>
					</TR>
				  </TABLE>";
		$minimum = ($page-1)*$pagesize;
		$this->minimum = $minimum;
		
		$this->display = $showpage;
		//$this->display = $total > $pagesize ? $showpage : "";
		$this->pagesize = $pagesize;
	}
	 /* 分页显示函数
	  */
	function display(){
		echo $this->display;
	}
	
	static function clearQuery($querystring,$remove_array = array()) {
		$query_string = @explode('&',$querystring);
		foreach($query_string as $v){
			$data = @explode('=',$v);
			if(!isset($data[1])) $data[1] = '';
			$querys[$data[0]] = $data[1];
			unset($data);
		}
		foreach($querys as $k => $v){
			if (in_array($k,$remove_array)) {
				continue;
			}
			if($k && $v) $data[] = "$k=$v";
		}
		Return @implode('&', $data);
	}
}
